package com.yun.platform.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yun.platform.config.dto.QueryDto;
import com.yun.platform.config.dto.RData;
import com.yun.platform.config.enums.QueryEnum;
import com.yun.platform.iot.dto.BaseDto;
import com.yun.platform.util.StringsUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Collection;
import java.util.List;

/**
 * @author hanson
 * @param <E> dto
 * @param <T> entity
 */
@Slf4j
public abstract class ListController<E extends BaseDto, T extends Object> extends BaseCURDController<T> {


    @ApiOperation("查询所有数据接口,传入dto")
    @PostMapping("/findall")
    public RData findAll(@RequestBody E dto) {
        //准备查询条件
        QueryWrapper<T> queryWrapper = new QueryWrapper<T>();
        List<QueryDto> queryDtoList = dto.getQueryDtoList();
        queryDtoList.forEach(item -> {
            if (item.getCondition() == QueryEnum.eq) {
                queryWrapper.eq(StringsUtils.xX2x_x(item.getKey()), item.getValue());
            } else if (item.getCondition() == QueryEnum.like) {
                queryWrapper.like(StringsUtils.xX2x_x(item.getKey()), item.getValue());
            } else if (item.getCondition() == QueryEnum.ge) {
                queryWrapper.ge(StringsUtils.xX2x_x(item.getKey()), item.getValue());
            } else if (item.getCondition() == QueryEnum.le) {
                queryWrapper.le(StringsUtils.xX2x_x(item.getKey()), item.getValue());
            } else if (item.getCondition() == QueryEnum.in) {
                queryWrapper.in(StringsUtils.xX2x_x(item.getKey()), (Collection) item.getValue());// "value": [1,2,3]
            }
        });

        return RData.success(getService().list(queryWrapper));
    }


    @ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping("/listpage")
    public RData listPage(@RequestBody E dto) {
        //设置分页条件
        Page page = new Page(dto.getPage(), dto.getSize());
        //准备查询条件
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();

        List<QueryDto> queryDtoList = dto.getQueryDtoList();
        queryDtoList.forEach(item -> {
            if (item.getCondition() == QueryEnum.eq) {
                queryWrapper.eq(StringsUtils.xX2x_x(item.getKey()), item.getValue());
            } else if (item.getCondition() == QueryEnum.like) {
                queryWrapper.like(StringsUtils.xX2x_x(item.getKey()), item.getValue());
            } else if (item.getCondition() == QueryEnum.ge) {
                queryWrapper.ge(StringsUtils.xX2x_x(item.getKey()), item.getValue());
            } else if (item.getCondition() == QueryEnum.le) {
                queryWrapper.le(StringsUtils.xX2x_x(item.getKey()), item.getValue());
            } else if (item.getCondition() == QueryEnum.in) {
                queryWrapper.in(StringsUtils.xX2x_x(item.getKey()), (Collection) item.getValue());// "value": [1,2,3]
            }

        });
        IPage iPage = getService().page(page, queryWrapper);
        return RData.success(iPage);
    }


}
